/**  
* @Title: VideoTeachingController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年6月22日  
*/
package com.fortune.rainshine.service.controller;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.service.AliOssService;
import com.fortune.rainshine.service.service.VideoTeachingService;
import com.fortune.rainshinecommon.base.AliOssParmsModel;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.AliOssTypeEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ProductModel;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.service.VideoTeachingModel;
import com.fortune.rainshinecommon.util.OssFileNameUtils;

/**  
* @ClassName: VideoTeachingController  
* @Description: TODO(视频教学)  
* @author 张鑫
* @phone 17561783855
* @date 2019年6月22日  
*    
*/
@Controller
@RequestMapping("/service/video")	
public class VideoTeachingController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(ShopController.class);
	@Autowired
	private VideoTeachingService service;
	@Autowired
	private AliOssService aliOssService;
	
	@RequiresPermissions("video:view")
	@RequestMapping()
	public String stock(Model model, HttpServletRequest request) {
		//获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		//获取登录用户的按钮展示情况
		model.addAttribute("add", authorities.contains("video:add"));
		model.addAttribute("del", authorities.contains("video:delete"));
		
		List<VideoTeachingModel> videos = (List<VideoTeachingModel>) service.list();
		model.addAttribute("videos", videos);
		return "video/videoList.html";
	}
	
	@RequestMapping("/editForm")
	public String addUser(String shop,Model model) {
		return "video/video_form.html";
	}
	
	@RequiresPermissions("video:add")
    @ResponseBody
    @RequestMapping("/add")
    public MsgModel add(@RequestBody VideoTeachingModel model) {
		try {
			model.setUser(this.getLoginUser());

	    	return service.add(model);
		} catch (Exception e) {
			logger.error("add ====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    }
	
	@RequiresPermissions("video:upload")
	@ResponseBody
	@RequestMapping("/upload")
	public MsgModel upload(@RequestBody MultipartFile file) throws IOException {
		String filepath = null;
		try {
			
			String floder = AliOssTypeEnum.getValue(2);
			String rannum = UUID.randomUUID().toString().replaceAll("-", "");
			String fileName = OssFileNameUtils.getNewName(file, floder, rannum);
 			AliOssParmsModel model = new AliOssParmsModel(rannum, floder, fileName, file.getBytes());
			aliOssService.uploadFile(model);
			
			
			String path = OssFileNameUtils.getFilePath(floder, fileName);
			filepath = aliOssService.getUrl(path);
			return new MsgModel(ResultCode.SUCCESS, filepath);
		} catch (Exception e) {
			logger.error("upload ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}
	

	/**
	 * @Title: delete
	 * @Description: 删除
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年3月28日
	 */
	@RequiresPermissions("video:delete")
	@ResponseBody
	@RequestMapping("/delete")
	public MsgModel delete(@RequestBody VideoTeachingModel model) {
		try {
			return service.delete(model);
		} catch (Exception e) {
			logger.error("delete ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}
}
